//
//  SleepViewSegementedChartsViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/4.
//

import UIKit
import RxCocoa
import RxSwift
import MYModule

open class SleepSegementedChartsViewModel: ViewModel {
    
    public let revealAll: Bool
    public let viewWillAppear: ControlEvent<Bool>
    public let providerRelay: BehaviorRelay<SleepProvider?>
    
    public let dayChart = BehaviorRelay<SleepDataChartViewModel?>(value: nil)
    public let weekChart = BehaviorRelay<SleepDataChartViewModel?>(value: nil)
    public let monthChart = BehaviorRelay<SleepDataChartViewModel?>(value: nil)
    
    public let segementedDatasource = BehaviorRelay<[SleepDataChartViewModel?]>(value: [])
    
    
    public init(providerRelay: BehaviorRelay<SleepProvider?>, revealAll: Bool, viewWillAppear: ControlEvent<Bool>) {
        
        self.revealAll = revealAll
        self.viewWillAppear = viewWillAppear
        self.providerRelay = providerRelay
        
        super.init()
        
        reloadDayChart()
        reloadWeekChart()
        reloadMonthChart()
        
        Observable.combineLatest(dayChart, weekChart, monthChart, viewWillAppear).map {[weak self] (day, week, month, animted) ->[SleepDataChartViewModel?] in
            return self?.revealAll == false ? [day] : [day, week, month]
        }.bind(to: segementedDatasource).disposed(by: rx.disposeBag)
        
        
    }
    
    open func reloadDayChart() {
        
        Observable.combineLatest(providerRelay, viewWillAppear).map { provider, animated -> SleepDataChartViewModel? in
            
            
            guard let provider = provider else {
                return nil
            }
           
            let viewModel = SleepDataChartViewModel(date: provider.date,
                                                    defaultValue: provider.curDayDetail,
                                                    dataSource: [provider.curDayDetail],
                                                    chartType: .day)
            return viewModel
            
        }.debug().bind(to: dayChart).disposed(by: rx.disposeBag)
        
        
    }
    
    open func reloadWeekChart() {
        
       Observable.combineLatest(providerRelay, viewWillAppear).map { provider, animated -> SleepDataChartViewModel? in
           
           guard let provider = provider else {
               return nil
           }
            
            let viewModel = SleepDataChartViewModel(date: provider.date,
                                                    defaultValue: provider.weekRecord.first!,
                                                    dataSource: provider.weekRecord,
                                                    chartType: .week)
            return viewModel
       }.bind(to: weekChart).disposed(by: rx.disposeBag)
    }
    
    open func reloadMonthChart() {
        
        Observable.combineLatest(providerRelay, viewWillAppear).map { provider, animted -> SleepDataChartViewModel? in
            
            guard let provider = provider else {
                return nil
            }
            
            let viewModel = SleepDataChartViewModel(date: provider.date,
                                                    defaultValue: provider.monthRecord.first!,
                                                    dataSource: provider.monthRecord,
                                                    chartType: .month)
            return viewModel
        }.bind(to: monthChart).disposed(by: rx.disposeBag)
    }
    
}
